
use dashmap::DashMap;
use tokio::sync::oneshot;


use super::frame::Frame;

#[derive(Debug)]
pub struct FrameMap {
    m: DashMap<u16, oneshot::Sender<Frame>>,
}

impl FrameMap {
    pub fn new() -> FrameMap {
        FrameMap {
            m: DashMap::new(),
        }
    }

    pub fn insert(&self, require_id: u16) -> oneshot::Receiver<Frame> {
        let (tx, rx) = oneshot::channel();
        _ = self.m.insert(require_id, tx);
        rx
    }

    pub fn dispatch(&self, frame: Frame) {
        let id = frame.id();
        let tx = self.m.remove(&id);
        if let Some(tx) = tx {
            _ = tx.1.send(frame);
        }
    }
}
